﻿/*
 * fbasync - An Asynchronous Facebook API for .NET
 * Copyright (C) 2007 ittybittysoft
 * Email: info@ittybittysoft.com
 * Portions of the library are based on the Facebook Developer Toolkit
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Xml;

namespace fbasync
{
    [DataContract(), Serializable()]
    public class User
        : FacebookEntity
    {
        public readonly static IEnumerable<string> BasicProfile = new[] { "uid", "first_name", "last_name", "name", "is_app_user", "sex", "pic_square" };

        public readonly static IEnumerable<string> AllFields = BasicProfile.Union(new[] { "pic_small", "pic_big", "pic", "has_added_app", "sex", "birthday_date", "current_location", "notes_count", "religion", "timezone", "wall_count", "locale", "relationship_status", "hs_info", "education_history", "contact_email", "email", "proxied_email" });

        public const string DefaultOrderBy = "name";

        public const string FacebookEntity = "user";

        [DataMember()]
        public long? uid { get; set; }

        [DataMember()]
        public string first_name { get; set; }

        [DataMember()]
        public string last_name { get; set; }

        [DataMember()]
        public string name { get; set; }

        [DataMember()]
        public string pic_small { get; set; }

        [DataMember()]
        public string pic_big { get; set; }

        [DataMember()]
        public string pic_square { get; set; }

        [DataMember()]
        public string pic { get; set; }

        [DataMember()]
        public bool? is_app_user { get; set; }

        [Obsolete("This value is now equivalent to is_app_user.")]
        [DataMember()]
        public bool? has_added_app { get; set; }

        [DataMember()]
        public string sex { get; set; }

        [DataMember()]
        public string profile_url { get; set; }

        [DataMember()]
        public string relationship_status { get; set; }

        [DataMember()]
        public string significant_other_id { get; set; }

        [DataMember()]
        public string pic_small_with_logo { get; set; }

        [DataMember()]
        public string pic_big_with_logo { get; set; }

        [DataMember()]
        public string pic_square_with_logo { get; set; }

        [DataMember()]
        public string pic_with_logo { get; set; }

        [DataMember()]
        public string birthday_date { get; set; }

        [DataMember()]
        public string current_location { get; set; }

        [DataMember()]
        public int? notes_count { get; set; }

        [DataMember()]
        public string religion { get; set; }

        [DataMember()]
        public string timezone { get; set; }

        [DataMember()]
        public int? wall_count { get; set; }

        [DataMember()]
        public string locale { get; set; }

        [DataMember()]
        public string city { get; set; }

        [DataMember()]
        public string state { get; set; }

        [DataMember()]
        public string country { get; set; }

        [DataMember()]
        public string zip { get; set; }

        [DataMember()]
        public string high_school_info { get; set; }

        [DataMember()]
        public string education_history { get; set; }

        [DataMember()]
        public string contact_email { get; set; }

        [DataMember()]
        public string email { get; set; }

        [DataMember()]
        public string proxied_email { get; set; }

        public static IList<User> FromFqlResult(XmlDocument fqlResult, bool keepXml)
        {
            List<User> results = fqlResult.GetFqlResults(keepXml, e =>
                {
                    XmlElement currentLoc = null;
                    foreach (XmlNode node in e.ChildNodes)
                    {
                        if (node.Name == "current_location" && node.NodeType == XmlNodeType.Element)
                        {
                            currentLoc = (XmlElement)node;
                            break;
                        }
                    }
                    return new User
                    {
                        uid = e.GetChildLong("uid"),
                        first_name = e.GetChildText("first_name"),
                        last_name = e.GetChildText("last_name"),
                        name = e.GetChildText("name"),
                        pic_small = e.GetChildText("pic_small"),
                        pic_big = e.GetChildText("pic_big"),
                        pic_square = e.GetChildText("pic_square"),
                        pic = e.GetChildText("pic"),
                        is_app_user = e.GetChildBool("is_app_user"),
                        has_added_app = e.GetChildBool("has_added_app"),
                        sex = e.GetChildText("sex"),
                        profile_url = e.GetChildText("profile_url"),
                        relationship_status = e.GetChildText("relationship_status"),
                        significant_other_id = e.GetChildText("significant_other_id"),
                        pic_small_with_logo = e.GetChildText("pic_small_with_logo"),
                        pic_big_with_logo = e.GetChildText("pic_big_with_logo"),
                        pic_square_with_logo = e.GetChildText("pic_square_with_logo"),
                        pic_with_logo = e.GetChildText("pic_with_logo"),
                        birthday_date = e.GetChildText("birthday_date"),
                        current_location = e.GetChildText("current_location"),
                        notes_count = e.GetChildInt("notes_count"),
                        religion = e.GetChildText("religion"),
                        timezone = e.GetChildText("timezone"),
                        wall_count = e.GetChildInt("wall_count"),
                        locale = e.GetChildText("locale"),
                        city = currentLoc.GetChildText("city"),
                        state = currentLoc.GetChildText("state"),
                        country = currentLoc.GetChildText("country"),
                        zip = currentLoc.GetChildText("zip"),
                        high_school_info = e.GetChildText("hs_info"),
                        education_history = e.GetChildText("education_history"),
                        contact_email = e.GetChildText("contact_email"),
                        email = e.GetChildText("email"),
                        proxied_email = e.GetChildText("proxied_email"),
                    };
                });
            return results;
        }
    }
}
